<?php
require '../script/mysqlconfig.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 获取用户输入的用户名和密码
    $name = $_POST['name'];
    $pass = $_POST['pass'];
    $qpass = $_POST['qpass'];
    $email = $_POST['email'];

    /* 判断用户名是否为空*/
    if (empty($_POST["name"])) {
        die("用户名不能为空！");
    }
    /* 对密码进行规则验证*/
    if (strlen($_POST["pass"]) < 6) {
        die("密码不能小于6位!!");
    }
    /* 判断输入的两次密码是否一致*/
    if ($_POST["pass"] !== $_POST["qpass"]) {
        die("两次输入的密码不一致！");
    }
    /* 验证邮箱的有效性*/
    if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
        die("请输入有效的邮箱格式!");
    }
    /* 对密码加密，保证数据的安全*/
    $pass_hash = password_hash($_POST["pass"], PASSWORD_DEFAULT);

    /* 连接数据库*/
    $mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
    if ($mysqli->connect_error) {
        die("数据库连接错误：" . $mysqli->connect_error);
    }
    /* 向数据表中传入用户的数据，方便后面对登录进行验证*/
    if (isset($_POST["reg"])) {
        $sql = "INSERT INTO users (name,password,info) VALUES ('$name','$pass_hash','$email')";
//        $sql = "INSERT INTO users (name,password,info) VALUES ('$name','$pass','$email')";
        $mysqli->query($sql);
        if ($mysqli->affected_rows > 0) {
//            echo "注册成功";
            echo "<script>alert('注册成功!');history.go(-1);</script>";
        } else {
            die("注册失败: " . $mysqli->connect_error);
        }
    }
    // 关闭数据库连接
    mysqli_close($mysqli);
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <style>
form{
    max-width: 400px;
            height: 350px;
            background-color: rgba(247, 247, 247, 0.7);
            /*border: 1 solid #E4E4E4;*/
            margin: 100px auto;
            color: black;
            border-radius: 5px;
            padding: 25px 45px;
        }
        body{
    background-repeat: no-repeat;
            /*background-image: url(xingkong.png);*/
            background-size: 100%;
        }
        div{
    width: 100%;
    height: 35px;
            line-height: 35px;
            margin-top: 20px;
            position: relative;
        }
        input{
    position: absolute;
    right: 80px;
            width: 60%;
            height: 25px;
            line-height: 25px;
            border: 1px solid #DADADA;
            color: #888;
            outline: none;
            font-size: 12px;
            padding: 3px 3px 3px 5px;
            box-shadow: inset 0 1px 4px #ECECEC;
        }
        input[type="submit"]{
        width: 50%;
            height: 40px;
            line-height: 15px;
            font-size: 15px;
            background-color: #b5ccf3;
            border: none;
            box-shadow: none;
            border-radius: 10px;
            color: #FFF;
            position: relative;
            top: 50px;
            left: 50%;
            transform: translateX(-50%);
        }
        input[type="submit"]:hover{
    cursor: pointer;                          /*手型*/
    background-color: rgb(139, 205, 246);
        }
        a{
    width: 180px;
            display: block;
            margin: 80px auto;
        }
    </style>
</head>
<body>
<form action="../users/register.php" method="post" novalidate>
    <div>
        <label for="name">用户名:</label>
        <input type="text" id="name" name="name" placeholder="请输入用户名" required>
    </div>
    <div>
        <label for="pass">密 &nbsp;&nbsp;&nbsp; 码:</label>
        <input type="password" name="pass" id="pass" placeholder="请输入密码" required>
    </div>
    <div>
        <label for="qpass">密 &nbsp;&nbsp;&nbsp; 码:</label>
        <input type="password" name="qpass" id="qpass" placeholder="请输入密码" required>
    </div>
    <div>
        <label for="email">邮&nbsp;&nbsp;&nbsp;箱</label>
        <input type="email" id="email" name="email" placeholder="请输入邮箱" pattern="[^@]*@[^@]*[\.com$]" required>
    </div>
    <input type="submit" name="reg" value="注册">
    <a href="../login.php">已经有账号了，去登录</a>
</form>
</body>
</html>